package com.record;

import com.elite.groovy.dev.TxConfig;
import com.elite.groovy.kh.HttpClientUtil;
import com.elite.groovy.kh.KHDBConfig;
import com.elite.groovy.kh.KHUtil;
import com.record.ccod.CCODUtils;
import com.record.ccod.CCODUtils_WaRTC;
import com.record.shdx.SHDXRecUtils;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TP_RecordDev {
    private static final Log log = LogFactory.getLog(TP_RecordDev.class);

    private static JdbcTemplate jt = new JdbcTemplate(TxConfig.getDruidDataSource());

    public static void main(String[] args) throws Exception {

        List<String> recDataList = new ArrayList<>();

        // CCOD 需要转码浏览器才能播放
        recDataList.add("1909090002|3d5fec1f6a003246|25793973|000000"); // 0 http://jsdx.ccod.com:8099/g10/M00/CF/25/CmQA7F_sH9SALeuoAAA9hmoLec4989.wav
        recDataList.add("1909090002|3c619704ce11d23a|25793973|000000"); // 1 http://jsdx.ccod.com:8099/g10/M00/D8/37/CmQA6WGXBXGAZobmAAEt5oRskB4392.wav
        recDataList.add("1909090002|3d618e19280f8a49|25793973|000000"); // 2 http://jsdx.ccod.com:8099/g10/M00/E7/41/CmQA6WGOGXqAKFmUAAAzxvbIRXo371.wav
        recDataList.add("1909090002|3d618e18ed0f8a01|25793973|000000"); // 3 http://jsdx.ccod.com:8099/g10/M00/A4/49/CmQA6WGOGUOAZyOWAAA_RokGzDg363.wav

        // SHDX(上海电信)
        recDataList.add("2112200001|676406dbbalac545|25810066|000000");
        recDataList.add("2112200001|6764019a29196967|25810066|000000");
        recDataList.add("2112200001|676412bf391e692d|25810066|000000"); // 6

        // SHDX(上海电信) - 历史
        recDataList.add("1912110002|676192236746fea6|25794418|25794418");
        recDataList.add("1912110002|5c6191c63e464163|25794418|25794418");
        recDataList.add("1912110002|5c618ceb2942e852|25794418|25794418"); // 9

        // 新青牛CCOD WEBRTC 无需转码
        recDataList.add("6766ea66729cbe0d:2209080002:25811564"); // 10 http://jsdx.ccod.com:8099/g12/M00/DD/80/CmQATGbqZqWAUt9SAACcYIyVPPo288.wav
        recDataList.add("6766ea497f9cb6f0:2209080002:25811564"); // 11 http://jsdx.ccod.com:8099/g12/M00/35/FD/CmQATGbqSa6Aej01AACu8FfL4yI047.wav

        recDataList.add("1909090002|6764019a29196967|25810066|000000"); // 12 假录音
        log.debug("recDataList:" + recDataList);

       // getRecordType(recDataList.get(1), "");
        String url_Call = "http://127.0.0.1:8080/groovy/rec/getrec?recdata=" + KHUtil.encode(recDataList.get(2)) + "&multimode=1";
        log.debug("url_Call:" + url_Call);
        String res_Call = HttpClientUtil.get(url_Call);
        log.debug("res_Call:" + res_Call);

    }

    // Record录音平台识别
    public static void getRecordType(String recData, String multimode) {

        String rec_Res = "";
        log.debug("-系统录音获取 原始Get请求参数recdata:" + recData + " multimode:" + multimode);

        JdbcTemplate jt = new JdbcTemplate(TxConfig.getDruidDataSource());
        JSONObject REC_CFG_Main = JSONObject.fromObject(KHDBConfig.getParamConfig(jt, "REC_CFG_Main", "{\"1909090002\":\"doCCODRecord\",\"2209080002\":\"doCCODQuery\",\"2112200001\":\"doSHDXRecord\",\"1912110002\":\"doSHDXRecord\"}"));

        // 遍历基本信息
        String entId = "";
        String entId_type = "";
        Iterator iterator = REC_CFG_Main.keys();
        while (iterator.hasNext()) {
            entId = (String) iterator.next();

            if (recData.contains(entId + "|") || recData.contains(entId + ":")) {
                entId_type = KHUtil.GetSFJV(REC_CFG_Main, entId);
                break;
            }
        }

        // 如果未配置录音或未识别到EntID对应的录音则采用默认值
        if (KHUtil.isNull(entId_type)) {
            entId = "1909090002";
            entId_type = "doCCODQuery";
            log.warn("-系统录音获取 未配置或未识别出录音平台,使用默认值 EntID_type:" + entId_type);
        }

        if (entId_type.equalsIgnoreCase("doCCODRecord")) { // CCOD
            rec_Res = CCODUtils.doCCODRecord(jt, null, entId, recData, multimode);
        } else if (entId_type.equalsIgnoreCase("doCCODQuery")) { // CCOD 无控件
            rec_Res = CCODUtils_WaRTC.doCCODQuery(jt, null, "queryRecord", entId, recData, "", "", "", multimode); // query呼叫明细数据 queryRecord录音数据
        } else if (entId_type.equalsIgnoreCase("doSHDXRecord")) { // SHDX-1
            rec_Res = SHDXRecUtils.doSHDXRecord(jt, null, entId, recData, multimode);
        } else if (entId_type.equalsIgnoreCase("doSHDXRecord2")) { // SHDX-2
            rec_Res = SHDXRecUtils.doSHDXRecord2(jt, null, entId, recData, multimode);
        } else {
            rec_Res = "无法识别判断录音平台类型";
        }
        log.debug("-系统录音获取 API调用结果:" + rec_Res);

    }
}
